<?php

/*
	info: 用户预定结果查询
*/

define('IS_MEMBER_CHECK', TRUE);
require '../common.inc.php';

//flag 0-仅状态 1-包含我的预定信息 2-包含地址信息 4-包含活动信息
global_CheckPost(
	array(
		array('flag','int','','',0,0),
));

$webglobal['CPP']['id'] = api_GetTopActivityID();

$return['code'] = 0;
$return['data']['ischeckcode'] = false;
$return['data']['isReserved'] = false;

$webglobal['datainfo'] = $db->GetOne("SELECT U.sid,U.peoplenum,U.codetime,S.* FROM promotion_user U LEFT JOIN promotion_site S ON (U.sid=S.id) WHERE U.uid='{$user['m_uid']}' AND U.pid='{$webglobal['CPP']['id']}'");
if (!empty($webglobal['datainfo']))
{
	$return['data']['ischeckcode'] = true;
	if ($webglobal['datainfo']['peoplenum']>0 && !empty($webglobal['datainfo']['id']))
	{
		$return['data']['isReserved'] = true;
	}
}

//仅状态
if ($webglobal['CPP']['flag']==0)
{
	api_EchoReturn();
}
//包含我的预定信息
if ($return['data']['isReserved'] && ($webglobal['CPP']['flag'] & 1))
{
	$return['data']['myreserve'] = array(
		'peoplenum' => $webglobal['datainfo']['peoplenum'],
	);
}
//包含地址信息
if ($return['data']['isReserved'] && ($webglobal['CPP']['flag'] & 2))
{
	$tempalbums = array();
	$tempcovershowimg = array();
	$query1 = $db->Query("SELECT * FROM promotion_site_img WHERE sid='{$webglobal['datainfo']['id']}' ORDER BY imgorder ASC,id ASC");
	while ($tempdata1 = $db->FetchArray($query1))
	{
		$tempalbums[] = array(
			'id' => $tempdata1['id'],
			'imginfo' => $tempdata1['imginfo'],
			'imgfile' => $webconfig['webimageurl'].$tempdata1['imgfile'],
		);
		if (count($tempcovershowimg) < 2)
		{
			$tempcovershowimg[] = $webconfig['webimageurl'].$tempdata1['imgfile'];
		}
	}

	//读取活动的图片
	$webglobal['activityinfo'] = $db->GetOne("SELECT * FROM promotion WHERE id='{$webglobal['CPP']['id']}'");
	if (!empty($webglobal['activityinfo']['showimg']))
	{
		$tempcovershowimg[] = $webconfig['webimageurl'].$webglobal['activityinfo']['showimg'];
	}

	$return['data']['site'] = array(
		'siteid' => $webglobal['datainfo']['id'],
		'sitename' => $webglobal['datainfo']['sitename'],
		'begintime' => !empty($webglobal['datainfo']['begintime']) ? global_FormatTime($webglobal['datainfo']['begintime'], $webglobal['sitetimemode']) : '',
		'begintime_num' => $webglobal['datainfo']['begintime'],
		'reservetime_num' => $webglobal['datainfo']['begintime']-$webglobal['lastedittime'],
		'city' => $webglobal['datainfo']['city'],
		'address' => $webglobal['datainfo']['address'],
		'telephone' => $webglobal['datainfo']['telephone'],
		'info' => $webglobal['datainfo']['info'],
		'coverimg' => !empty($webglobal['datainfo']['coverimg']) ? $webconfig['webimageurl'].$webglobal['datainfo']['coverimg'] : '',
		'coverimgs' => $tempcovershowimg,
		'latitude' => $webglobal['datainfo']['latitude'],
		'longitude' => $webglobal['datainfo']['longitude'],
		'maxpeoplenum' => $webglobal['datainfo']['maxpeoplenum'],
		'curpeoplenum' => $webglobal['datainfo']['curpeoplenum'],
		'albums' => $tempalbums,
	);
}
//包含活动信息
if ($return['data']['ischeckcode'] && ($webglobal['CPP']['flag'] & 4))
{
	if (!isset($webglobal['activityinfo']))
	{
		$webglobal['activityinfo'] = $db->GetOne("SELECT * FROM promotion WHERE id='{$webglobal['CPP']['id']}'");
	}
	$tempcontent = api_FormatContent($webglobal['activityinfo']['content'], $webglobal['datainfo']['codetime']);
	$return['data']['activity'] = array(
		'id' => $webglobal['activityinfo']['id'],
		'title' => $webglobal['activityinfo']['title'],
		'giftpic' => $webconfig['webimageurl'].$webglobal['activityinfo']['gifpic'],
		'giftinfo' => $webglobal['activityinfo']['giftinfo'],
		'showimg' => $webconfig['webimageurl'].$webglobal['activityinfo']['showimg'],
		'preface' => $tempcontent[0],
		'content' => $tempcontent[1],
	);
}

api_EchoReturn();
